//
//  SurpriseShopVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/10.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class SurpriseShopVC: BaseViewController {
    
    var tableGun = true
    
    var dataModel : SurpriseShopDataModel?
    
    
    var bottomCell : SurpriseShopTableCell!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "鲸喜购"
        
        surpriseTable.register(UINib(nibName: StringFromClass(classType: SurpriseShopTopCell.self), bundle: nil), forCellReuseIdentifier: SurpriseShopTopCell.identifer())
        
        surpriseTable.register(UINib(nibName: StringFromClass(classType: SurpriseShopTextCell.self), bundle: nil), forCellReuseIdentifier: SurpriseShopTextCell.identifer())

        
        surpriseTable.register(UINib(nibName: StringFromClass(classType: SurpriseCountdownCell.self), bundle: nil), forCellReuseIdentifier: SurpriseCountdownCell.identifer())
        
        surpriseTable.register(SurpriseShopTableCell.self, forCellReuseIdentifier: SurpriseShopTableCell.identifer())
        
        surpriseTable.tableFooterView = UIView()
        
        surpriseTable.separatorStyle = .none
        
        
        weak var weakSelf = self
        
        surpriseTable.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }

        surpriseTable.mj_header.beginRefreshing()

//        surpriseTable.bounces = false
        
        //通知名称常量
        let NotifyChatMsgRecv = NSNotification.Name(rawValue:"leaveTop")
        

        let NotifyChatMsgRecv2 = NSNotification.Name(rawValue:"collectionViewItemDidTap")
        
        NotificationCenter.default.addObserver(forName: NotifyChatMsgRecv, object: nil, queue: OperationQueue.main) { (notice) in
            
            weakSelf?.tableGun = true
            weakSelf?.bottomCell.subtabGun = false
            
        }
        
        NotificationCenter.default.addObserver(forName: NotifyChatMsgRecv2, object: nil, queue: OperationQueue.main) { (notice) in
            
            let temp = SurpriseShopDetailVC()
            
            if let model = notice.object as? SupriseDataListModel{
                
                temp.odId = model.od_id
                
            }
            
            weakSelf?.navigationController?.pushViewController(temp, animated: true)
         
        }
        
        

        

       
    }
    
    deinit {
        
        surpriseVC = nil
        NotificationCenter.default.removeObserver(self)
    }
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
  
   
}

extension SurpriseShopVC{


    func loadData() {
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=one_dollar&a=index", parameters: nil) {[weak self] (responseObject, error)  in
            
            
            if error == nil {
                
                if responseObject == nil {
                    self?.surpriseTable.mj_header.endRefreshing()
                    return
                }
                
                self?.dataModel = SurpriseShopDataModel(dic: responseObject!)
                
                self?.surpriseTable.mj_header.endRefreshing()
                
                self?.surpriseTable.reloadData()
                
            }else{
                
                self!.surpriseTable.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
        
        
        
    }





}


extension SurpriseShopVC{


    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
     
        return 2
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModel == nil {
            return 0
        }
        
        if section == 0 {
            var temp = 1
            
            if dataModel!.pre_list.count > 0 {
                temp += 1
            }
            
            if dataModel!.headline_list.count > 0 {
                
                temp += 1
                
            }
            
            return temp
        } else {
            return 1
        }
        
        
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                
                let tempCell : SurpriseShopTopCell =  tableView.dequeueReusableCell(withIdentifier: SurpriseShopTopCell.identifer()) as! SurpriseShopTopCell
                
                
                tableCell = tempCell
            }else if indexPath.row == 1 {
                
                
                if dataModel!.headline_list.count == 0 && dataModel!.pre_list.count > 0{
                    
                    let tempCell : SurpriseCountdownCell =  tableView.dequeueReusableCell(withIdentifier: SurpriseCountdownCell.identifer()) as! SurpriseCountdownCell
                    
                    tempCell.model = dataModel?.pre_list
                    
                    tableCell = tempCell
                    
                    
                }else{
                    
                    let tempCell : SurpriseShopTextCell =  tableView.dequeueReusableCell(withIdentifier: SurpriseShopTextCell.identifer()) as! SurpriseShopTextCell
                    
                    tempCell.headList = dataModel?.headline_list
                    
                    tableCell = tempCell
                    
                }
                
                
                
                
                
            }else{
                
                let tempCell : SurpriseCountdownCell =  tableView.dequeueReusableCell(withIdentifier: SurpriseCountdownCell.identifer()) as! SurpriseCountdownCell
                
                tempCell.model = dataModel?.pre_list

                tableCell = tempCell
                
                
            }
            
        } else {
            bottomCell =  tableView.dequeueReusableCell(withIdentifier: SurpriseShopTableCell.identifer()) as! SurpriseShopTableCell
            
            //            bottomCell.list = dataModel?.datas?.list
            
            bottomCell.type_list = dataModel?.type_list
            
            tableCell = bottomCell
            
            
        }
        
        
        return tableCell!
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                
                return 108
                
            } else if indexPath.row == 1{
                
                if dataModel!.headline_list.count == 0 && dataModel!.pre_list.count > 0{
                    
                  return 100
                    
                }else{
                    
                  return 46
                }
        
            }else{
                return 100
            }
        } else {
            return KScreenH - 64

        }
        
        
        
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        QLPlusLine()
    }
    
    

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        if dataModel == nil{
        
        return
        
        }
        
        
        
        let subTabelOffset  = surpriseTable.rect(forSection: 1).origin.y
        
//        QL1(subTabelOffset)
        
//        QL1(scrollView.contentOffset.y)
        
        if scrollView.contentOffset.y >= subTabelOffset  - 64 {
            
//            QL1("111111")
            scrollView.contentOffset = CGPoint(x: 0, y: subTabelOffset  - 64 )
            if (tableGun) {
//                QL1("222222");
                
                tableGun = false
                bottomCell.subtabGun = true
            }
        }else{
//            QL1("3333333");
            
            if (!tableGun) { //子视图没到顶部
//                QL1("4444444")
                
                scrollView.contentOffset = CGPoint(x: 0, y: subTabelOffset  - 64 )
                
            }
            
        }

        
    }




}
